package csp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test2 {
	
	public static void main(String[] args) {
		//定义变量值
		List<String> variables = List.of("A","B","C","D","E","F","G","H");
		//定义值域
		Map<String,List<String>> domains = new HashMap<>();
		domains.put("A",List.of("0,0","0,1","0,2","0,3","0,4","0,5","0,6","0,7"));
		domains.put("B",List.of("1,0","1,1","1,2","1,3","1,4","1,5","1,6","1,7"));
		domains.put("C",List.of("2,0","2,1","2,2","2,3","2,4","2,5","2,6","2,7"));
		domains.put("D",List.of("3,0","3,1","3,2","3,3","3,4","3,5","3,6","3,7"));
		domains.put("E",List.of("4,0","4,1","4,2","4,3","4,4","4,5","4,6","4,7"));
		domains.put("F",List.of("5,0","5,1","5,2","5,3","5,4","5,5","5,6","5,7"));
		domains.put("G",List.of("6,0","6,1","6,2","6,3","6,4","6,5","6,6","6,7"));
		domains.put("H",List.of("7,0","7,1","7,2","7,3","7,4","7,5","7,6","7,7"));
		//定义约束（相对定义变量值和定义值域来说是最难的部分）
		CSP<String,String> csp = new CSP<>(variables,domains);
		csp.addConstraint(new QueenConstraint("A","B"));
		csp.addConstraint(new QueenConstraint("A","C"));
		csp.addConstraint(new QueenConstraint("A","D"));
		csp.addConstraint(new QueenConstraint("A","E"));
		csp.addConstraint(new QueenConstraint("A","F"));
		csp.addConstraint(new QueenConstraint("A","G"));
		csp.addConstraint(new QueenConstraint("A","H"));
		
		csp.addConstraint(new QueenConstraint("B","A"));
		csp.addConstraint(new QueenConstraint("B","C"));
		csp.addConstraint(new QueenConstraint("B","D"));
		csp.addConstraint(new QueenConstraint("B","E"));
		csp.addConstraint(new QueenConstraint("B","F"));
		csp.addConstraint(new QueenConstraint("B","G"));
		csp.addConstraint(new QueenConstraint("B","H"));
		
		csp.addConstraint(new QueenConstraint("C","A"));
		csp.addConstraint(new QueenConstraint("C","B"));
		csp.addConstraint(new QueenConstraint("C","D"));
		csp.addConstraint(new QueenConstraint("C","E"));
		csp.addConstraint(new QueenConstraint("C","F"));
		csp.addConstraint(new QueenConstraint("C","G"));
		csp.addConstraint(new QueenConstraint("C","H"));
		
		csp.addConstraint(new QueenConstraint("D","A"));
		csp.addConstraint(new QueenConstraint("D","B"));
		csp.addConstraint(new QueenConstraint("D","C"));
		csp.addConstraint(new QueenConstraint("D","E"));
		csp.addConstraint(new QueenConstraint("D","F"));
		csp.addConstraint(new QueenConstraint("D","G"));
		csp.addConstraint(new QueenConstraint("D","H"));
		
		csp.addConstraint(new QueenConstraint("E","A"));
		csp.addConstraint(new QueenConstraint("E","B"));
		csp.addConstraint(new QueenConstraint("E","C"));
		csp.addConstraint(new QueenConstraint("E","D"));
		csp.addConstraint(new QueenConstraint("E","F"));
		csp.addConstraint(new QueenConstraint("E","G"));
		csp.addConstraint(new QueenConstraint("E","H"));
		
		csp.addConstraint(new QueenConstraint("F","A"));
		csp.addConstraint(new QueenConstraint("F","B"));
		csp.addConstraint(new QueenConstraint("F","C"));
		csp.addConstraint(new QueenConstraint("F","D"));
		csp.addConstraint(new QueenConstraint("F","E"));
		csp.addConstraint(new QueenConstraint("F","G"));
		csp.addConstraint(new QueenConstraint("F","H"));
		
		csp.addConstraint(new QueenConstraint("G","A"));
		csp.addConstraint(new QueenConstraint("G","B"));
		csp.addConstraint(new QueenConstraint("G","C"));
		csp.addConstraint(new QueenConstraint("G","D"));
		csp.addConstraint(new QueenConstraint("G","E"));
		csp.addConstraint(new QueenConstraint("G","F"));
		csp.addConstraint(new QueenConstraint("G","H"));
		
		csp.addConstraint(new QueenConstraint("H","A"));
		csp.addConstraint(new QueenConstraint("H","B"));
		csp.addConstraint(new QueenConstraint("H","C"));
		csp.addConstraint(new QueenConstraint("H","D"));
		csp.addConstraint(new QueenConstraint("H","E"));
		csp.addConstraint(new QueenConstraint("H","F"));
		csp.addConstraint(new QueenConstraint("H","G"));
		//解决方案
		Map<String,String> solution = csp.backtrackingSearch(new HashMap<String,String>());
		if (solution == null || solution.size() == 0) {
			System.out.println("No solution found!");
		} else {
			//{A=0,0, B=1,4, C=2,7, D=3,5, E=4,2, F=5,6, G=6,1, H=7,3}
			System.out.println(solution);
		}
	}
	
}
